﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.Utils;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using Assessment.Management.UI.Admin.Upload;
using Assessment.Management.SharedComponent.Params.MenuManagers;
using System.Security.Cryptography;

namespace Assessment.Management.UI.Admin.UserControls.Permissions
{
    public partial class PermissionAdd : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private bool ValidateDataToAdd()
        {
            bool kt = true;
          
            if (ddlpermission.SelectedItem.ToString() == "-------Chọn quyền--------" || txtEmail.Text == ""||txtPass.Text==""||txtPass2.Text==""||txtUser.Text==""||txtPass.Text!=txtPass2.Text)
            {
                kt = false;
            }
            return kt;
        }
        private bool CheckExitsAccData()
        {
            AccountParam param = new AccountParam(FunctionType.CheckExitsAccData);
            Account acc = new Account();
            acc.Username = txtUser.Text;
            param.Account = acc;
            MainController.Provider.Execute(param);
            bool kt = false;
            if (param.Account.Username!="")
            {
                kt = true;
            }
            return kt;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateDataToAdd())
            {
                if (CheckExitsAccData())
                {
                    lblMessage.Text = "<font color='Red'>Đã tồn tại dữ liệu này. Vui lòng kiểm tra lại</font>";
                    txtUser.Text = "";
                }
                else
                {

                    SaveAccount();
                    lblMessage.Text = "<font color='Red'>Thêm thành công !!!</font>";
                }
            }
            else
            {
                lblMessage.Text = "<font color='Red'>Thông tin chưa đầy đủ. Vui lòng kiểm tra lại</font>";
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (ValidateDataToAdd())
            {
                if (CheckExitsAccData())
                {
                    lblMessage.Text = "<font color='Red'>Đã tồn tại dữ liệu này. Vui lòng kiểm tra lại</font>";
                }
                else
                {
                    SaveAccountAndNew();
                    lblMessage.Text = "<font color='Red'>Thêm thành công !!!</font>";
                }

            }
            else
            {
                lblMessage.Text = "<font color='Red'>Thông tin chưa đầy đủ. Vui lòng kiểm tra lại</font>";
            }
        }
        //Lưu môn học xuống csdl
        private void SaveAccount()
        {
            AccountParam param = new AccountParam(FunctionType.CreateAccount);
            param.Account = GetItemOnForm();
            MainController.Provider.Execute(param);
            Response.Redirect("UniAdmin.aspx?do=permission");
        }
        private void SaveAccountAndNew()
        {
            AccountParam param = new AccountParam(FunctionType.CreateAccount);
            param.Account = GetItemOnForm();
            MainController.Provider.Execute(param);
            Response.Redirect("UniAdmin.aspx?do=permissionadd");
        }

        //Lấy dữ liệu nhập vào
        private Account GetItemOnForm()
        {
            Account acc = new Account();
            acc.Username = txtUser.Text.Trim();
            if(ddlpermission.SelectedItem.ToString()=="Admin")
            {
                acc.Permission=1;
            }
             if(ddlpermission.SelectedItem.ToString()=="Manager")
            {
                acc.Permission=2;
            }
             if(ddlpermission.SelectedItem.ToString()=="User")
            {
                acc.Permission=3;
            }
             if (txtPass2.Text==txtPass.Text)
             {
                 acc.Password = EncryptString(txtPass.Text,"123");
             }
             acc.Email = txtEmail.Text;
            acc.CreatedBy = SMX.smx_CreatedBy;
            acc.CreatedDTG = DateTime.Now;
            acc.Deleted = SMX.smx_IsNotDeleted;
            acc.Version = SMX.smx_FirstVersion;
            return acc;
        }
        //Hàm mã hóa chuỗi
        public static string EncryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            // Buoc 1: Bam chuoi su dung MD5
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
            // Step 2. Tao doi tuong TripleDESCryptoServiceProvider moi
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            // Step 3. Cai dat bo ma hoa
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            // Step 4. Convert chuoi (Message) thanh dang byte[]
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            // Step 5. Ma hoa chuoi
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                // Xoa moi thong tin ve Triple DES va HashProvider de dam bao an toan
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            // Step 6. Tra ve chuoi da ma hoa bang thuat toan Base64
            return Convert.ToBase64String(Results);
        }
    }
}